package com.ghck.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.ghck.server.model.AjkAccount;
import com.ghck.server.model.query.AjkAccountQuery;
/**
 * 账号接口认证表  <br>
 * ajk_account <br>
 * v1.0 2016-05-01
 * @see com.ajk.server.dao.AjkAccountMapper
 */
 public class AjkAccountProvider {
 
 	/**
 	* 新增 账号接口认证表
 	*/
 	public String insert(AjkAccount domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into ajk_account(create_time,last_modify_time");
	 	if(domain.getUserId()!=null){
			sb.append(",user_id");
		}
	 	if(domain.getRole()!=null){
			sb.append(",role");
		}
	 	if(domain.getAccessId()!=null){
			sb.append(",access_id");
		}
	 	if(domain.getAccessKey()!=null){
			sb.append(",access_key");
		}
	 	if(domain.getIsBlock()!=null){
			sb.append(",is_block");
		}
	 	if(domain.getAccountExpiredTime()!=null){
			sb.append(",account_expired_time");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getUserId()!=null){
			sb.append(",#{userId}");
		}
	 	if(domain.getRole()!=null){
			sb.append(",#{role}");
		}
	 	if(domain.getAccessId()!=null){
			sb.append(",#{accessId}");
		}
	 	if(domain.getAccessKey()!=null){
			sb.append(",#{accessKey}");
		}
	 	if(domain.getIsBlock()!=null){
			sb.append(",#{isBlock}");
		}
	 	if(domain.getAccountExpiredTime()!=null){
			sb.append(",#{accountExpiredTime}");
		}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 账号接口认证表
 	*/
	public String update(AjkAccount domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update ajk_account set last_modify_time = now() ");
		
		if(domain.getUserId()!=null){
			sb.append(",user_id=#{userId}");
		}
		if(domain.getRole()!=null){
			sb.append(",role=#{role}");
		}
		if(domain.getAccessId()!=null){
			sb.append(",access_id=#{accessId}");
		}
		if(domain.getAccessKey()!=null){
			sb.append(",access_key=#{accessKey}");
		}
		if(domain.getIsBlock()!=null){
			sb.append(",is_block=#{isBlock}");
		}
		if(domain.getAccountExpiredTime()!=null){
			sb.append(",account_expired_time=#{accountExpiredTime}");
		}
		
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<AjkAccount>> map){
		StringBuffer sb = new StringBuffer();
		List<AjkAccount> ajkAccounts = map.get("list");
		sb.append("insert into ajk_account(create_time,last_modify_time,user_id,role,access_id,access_key,is_block,account_expired_time) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].userId}"
	    		+ ",#'{'list[{0}].role}"
	    		+ ",#'{'list[{0}].accessId}"
	    		+ ",#'{'list[{0}].accessKey}"
	    		+ ",#'{'list[{0}].isBlock}"
	    		+ ",#'{'list[{0}].accountExpiredTime}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(ajkAccounts)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  ajkAccounts.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 账号接口认证表
 	*/
	public String findByQuery(AjkAccountQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from ajk_account a ");
		
		//TODO: add your logic
		
		if(StringUtils.isEmpty(query.getSortStatment())){
			sb.append(" order by create_time desc ");
		}else{
			sb.append(" ").append(query.getSortStatment());
		}
		if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(AjkAccountQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from ajk_account a ");
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 账号接口认证表
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from ajk_account where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 	public String findByUserIds(Map<String, List<Integer>> map)
 	{
 		StringBuilder sb = new StringBuilder();
		sb.append("select * from ajk_account where ");
		List<Integer> list = map.get("list");
		if (CollectionUtils.isNotEmpty(list)) {
			sb.append(" user_id in (#{list[0]}");
			for (int i = 1; i < list.size(); i++) {
				sb.append(",#{list[").append(i).append("]}");
			}
			sb.append(")");
		}else{
			sb.append(" 1<0 ");
		}
		return sb.toString();
 	}
 }	